import java.io.*;
import java.util.Scanner;
/*
    进行普通文件的复制
 */

public class Demo2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入需要复制的文件路径: ");
        String in = scanner.next();
        File inPath = new File(in);
        if(inPath.isFile()) {
            if(!inPath.exists()) {
                System.out.print("传入地址文件不存在,重新终止: ");
            }
        }else {
            System.out.print("传入路径为文件夹,程序终止: ");
            return;
        }
        // 到这里,文件已经获取到了,接下来就是获取目录文件
        System.out.print("请输入目标文件地址: ");
        String os = scanner.next();
        File osPath = new File(os);
        if(osPath.isFile()) {
            if(!osPath.exists()) {
                System.out.print("传入地址文件不存在,重新终止: ");
            }
            System.out.print("请问是否需要覆盖目标文件 (y/n): ");
            String isWriter = scanner.next();
            if(isWriter.equalsIgnoreCase("n")) {
                System.out.println("停止复制");
                return;
            }
        }else {
            System.out.println("传入路径为文件夹,程序终止");
            return;
        }
        // 到这里,已经确认 目标文件和目的文件都是已经存在的了,接下来开始复制
        try(FileInputStream read  = new FileInputStream(inPath)) {
            try(FileOutputStream writer = new FileOutputStream(osPath)) {
                byte[] ret = new byte[1024];
                int len = 0;
                while(true) {
                    len = read.read(ret);
                    if(len == -1) {
                        break;
                    }
                    writer.write(ret,0,len);
                }

            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("复制完成");
    }
}
